<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

   <head>
      <title>Add new match</title>
      <meta http-equiv="generator" content="PHP Designer 2006 - BETA" />
      <link href="default.css" rel="stylesheet" type="text/css" media="screen" />
   </head>

   <body>
         <div id="header">
	            <div id="logo">
		          <h1>FLeTaG</h1>
              </div>
         <div id="menu">
		     <ul>
             <li class="current_page_item"><a href="/fletag/scoreboard.php">Scoreboard</a></li>
			       <li><a href="http://snafu.org.uk/fletag/archive.php">Archive</a></li>
			       <li class="last"><a href="#">About</a></li>
       </ul>
	     </div>
      </div>
<?php

//Init variables
$t1_score = $_POST["t1_score"];
$t2_score = $_POST["t2_score"];
$game_date = $_POST["game_date"];
$match_type = $_POST["match_type"];
$t1_win = 0;
$t1_lost = 0;
$t2_win = 0;
$t2_lost = 0;
$draw = 0;

//Assign the dropdown selections for team1 to variables
$index = 0;
for ($d=0;$d<7;$d++)
{
  if ($_POST["t1_drop$d"] != 'NA')
  {
     $team1_array[$index] = $_POST["t1_drop$d"];
     $index++;
  }
}
$team1_size = count($team1_array);

//Assign the dropdown selections for team2 to variables
$index = 0;
for ($d=0;$d<7;$d++)
{
  if ($_POST["t2_drop$d"] != 'NA')
  {
     $team2_array[$index] = $_POST["t2_drop$d"];
     $index++;
  }
}
$team2_size = count($team2_array);

//We need the DB code and the functions
require ('functions.inc.php');
require ('dbconn.inc.php');

if (!isset($_POST['submit']))
{ // if page is not submitted to itself generate the form

print ("<div id=page><div id=content><div class=post><div class=title><h2><a href=#>Enter new match</a></h2>");
print ("</div></div></div>");


  //The following is used to populate the dropdowns
  $get_players_sql = "SELECT player
		                  FROM players";
		                  
  $players_result = @mysql_query($get_players_sql);

          if (!$players_result)
	        {
  	       exit('<p>There was an error performing the query. Additional details: ' . mysql_error() . '</p>');
	        }

  //Read data into an array and then step through for each person
  $row_count = 0;
  while ($row = mysql_fetch_array($players_result))
  {
        $players[$row_count] = $row['player'];
        $row_count++;
  }
  
  //Output the dropdowns and score boxes. NB Need to add checking to score input
  print ("Team 1<br />");
  print ("<form name=\"addmatch\" action=\"$PHP_SELF\" method=\"POST\">");
  for ($d=0;$d<7;$d++)
  {
        print ("<select name=\"t1_drop$d\">");
        print ("<option value=\"NA\">NA</option>");
        for ($p = 0; $p < count($players); $p++)
        {
              print("<option value=\"$players[$p]\">$players[$p]</option>");
        }
        print ("</select>");
  }
  
  print ("<br /><br />");
  print ("Team 1 Score:<input type=\"text\" size=\"2\" maxlength=\"2\" name=\"t1_score\"><br /><br />");
  print ("Team 2<br /><br />");
          
  for ($d=0;$d<7;$d++)
  {
        print ("<select name=\"t2_drop$d\">");
        print ("<option value=\"NA\">NA</option>");
        for ($p = 0; $p < count($players); $p++)
        {
              print("<option value=\"$players[$p]\">$players[$p]</option>");
        }
        print ("</select>");


  }
  print ("<br /><br />Team 2 Score:<input type=\"text\" size=\"2\" maxlength=\"2\" name=\"t2_score\"><br />");
  print ("<br /><br /><select name=\"match_type\">");
  print ("<option value=\"indoor\">Indoor</option>");
  print ("<option value=\"outdoor\">Outdoor</option>");
  print ("</select>");
  print ("<br /><br />Game date (YYYY-MM-DD):<input type=\"text\" size=\"10\" maxlength=\"10\" name=\"game_date\"><br />");



  print ("<br /><input type=\"submit\" value=\"submit\" name=\"submit\">");
  print ("</form>");
}
//The following runs if the form has been submitted
else
{
print ("<div id=page><div id=content><div class=post><div class=title><h2><a href=#>match results</a></h2>");
print ("</div></div></div>");
//Calculate the points to award to each team
 $team1_points = calc_points($t1_score,$t2_score,$team1_size,$team2_size);
 $team2_points = calc_points($t2_score,$t1_score,$team2_size,$team1_size);

//Output some text
 print ("<p>Points awarded to team1: $team1_points</p><br />");
 print ("<p>Points awarded to team2: $team2_points</p><br />");

 // Update the games table
 
 $team1_concat = '';
 $team2_concat = '';

  for ($d=0;$d<count($team1_array);$d++)
  {
   if ($d == 0)
   {
    $team1_concat = $team1_concat . $team1_array[$d];
   }
   else
   {
    $team1_concat = $team1_concat . "," . $team1_array[$d];
   }
  }
 
  for ($d=0;$d<count($team2_array);$d++)
  {
   if ($d == 0)
   {
    $team2_concat = $team2_concat . $team2_array[$d];
   }
   else
   {
    $team2_concat = $team2_concat . "," . $team2_array[$d];
   }
  }

 
 
 print ("<p> Adding $team1_points to team1 and $team2_points to team2</p><br>");
 
$update_games_sql = "INSERT INTO games SET
                    date = '$game_date',
                    score1 = $t1_score,
                    score2 = $t2_score,
                    team1 = '$team1_concat',
                    team2 = '$team2_concat',
                    points1 = $team1_points,
                    points2 = $team2_points,
                    matchtype = '$match_type'";

$games_result = @mysql_query($update_games_sql);

if (!$games_result)
     {
  	       exit("<p>Error adding data game. Additional details: " . mysql_error() . "</p><br />");
     }

}
    print ("</body>");
    print ("</html>");
?>